<?php

/**
 * This is the model class for table "turma".
 *
 * The followings are the available columns in table 'turma':
 * @property integer $id
 * @property integer $classe_id
 * @property integer $polo_id
 * @property integer $ano
 * @property integer $responsavel_id
 * @property string $nome
 * @property string $turno
 * @property string $unica
 * @property integer $vagas
 *
 * The followings are the available model relations:
 * @property Aula[] $aulas
 * @property Funcionario $responsavel
 * @property Polo $polo
 * @property Classe $classe
 * @property TurmaHasAluno[] $turmaHasAlunos
 */
class Turma extends CActiveRecord
{
	public $search_responsavel;
	public $search_classe;

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'turma';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
				array('classe_id, polo_id, ano, nome, vagas', 'required', 'message'=>'Campo {attribute} está vazio.'),
				array('classe_id, polo_id, ano, responsavel_id, vagas', 'numerical', 'integerOnly'=>true, 'message'=>'Campo {attribute} deve conter apenas números.'),
				array('nome, turno, unica', 'length', 'max'=>45),
				// The following rule is used by search().
				// @todo Please remove those attributes that should not be searched.
				array('id, classe_id, polo_id, ano, responsavel_id, nome, turno, unica, vagas, search_responsavel, search_classe', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
				'planos' => array (self::HAS_MANY, 'PlanoTurma', 'turma_id'),
				'aulas' => array(self::HAS_MANY, 'Aula', 'turma_id'),
				'responsavel' => array(self::BELONGS_TO, 'Funcionario', 'responsavel_id'),
				'polo' => array(self::BELONGS_TO, 'Polo', 'polo_id'),
				'classe' => array(self::BELONGS_TO, 'Classe', 'classe_id'),
				'turmaHasAlunos' => array(self::HAS_MANY, 'TurmaHasAluno', 'turma_int'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
				'id' => 'ID',
				'classe_id' => 'Classe',
				'polo_id' => 'Polo',
				'ano' => 'Ano',
				'responsavel_id' => 'Responsavel',
				'nome' => 'Nome',
				'turno' => 'Turno',
				'unica' => 'Unica',
				'vagas' => 'Vagas',
				'search_responsavel' => 'Responsavel',
				'search_classe' => 'Categoria',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search($parameter, $user_id)
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;
		if($user_id != 1) {
			$criteria->addCondition('t.polo_id = :polo_id');
			$criteria->params = array(':polo_id' => $parameter);
		}

		$criteria->with=array('responsavel', 'classe');
		if(strlen($this->search_responsavel))
			$criteria->addSearchCondition('responsavel.pessoa.nome',$this->search_responsavel,true);

		if(strlen($this->search_classe))
			$criteria->addSearchCondition('classe.nome',$this->search_classe,true);

		$criteria->compare('id',$this->id);
		$criteria->compare('classe_id',$this->classe_id);
		$criteria->compare('polo_id',$this->polo_id);
		$criteria->compare('ano',$this->ano);
		$criteria->compare('responsavel_id',$this->responsavel_id);
		$criteria->compare('nome',$this->nome,true);
		$criteria->compare('turno',$this->turno,true);
		$criteria->compare('unica',$this->unica,true);
		$criteria->compare('vagas',$this->vagas);

		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
		));
	}

	public function search_by_funcionario($user_id) {
		$criteria=new CDbCriteria;
		if($user_id > 0) {
			$criteria->addCondition('t.responsavel_id = :responsavel_id');
			$criteria->params = array(':responsavel_id' => $user_id);
		}

		if(strlen($this->search_classe))
			$criteria->addSearchCondition('classe.nome',$this->search_classe,true);

		$criteria->compare('id',$this->id);
		$criteria->compare('classe_id',$this->classe_id);
		$criteria->compare('polo_id',$this->polo_id);
		$criteria->compare('ano',$this->ano);
		$criteria->compare('responsavel_id',$this->responsavel_id);
		$criteria->compare('nome',$this->nome,true);
		$criteria->compare('turno',$this->turno,true);
		$criteria->compare('unica',$this->unica,true);
		$criteria->compare('vagas',$this->vagas);

		return new CActiveDataProvider($this, array(
				'criteria'=>$criteria,
		));
	}

	public function delete(){
		PlanoTurma::model()->deleteAllByAttributes(array('turma_id'=>$this->id));
		Aula::model()->deleteAllByAttributes(array('turma_id'=>$this->id));
		$alunosNaTurma = TurmaHasAluno::model()->findAllByAttributes(array('turma_int'=>$this->id));

		if($alunosNaTurma != null && !empty($alunosNaTurma)) {
			foreach($alunosNaTurma as $aluno) {	
				$aluno->delete();
			}
		}


		parent::delete();
	}

	public function findResponsavelNome() {

		$responsavel = Funcionario::model()->findByAttributes(array('id'=> $this->responsavel_id));
		$this->responsavel_nome = Pessoa::model()->findByAttributes(array('id'=> $responsavel->pessoa_id))->nome;
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Turma the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
